package ch.abcm.remsmobile.REMSMobile;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import ch.abcm.remsmobile.REMSMobile.externnotifier.ExternNotifierController;
import ch.abcm.remsmobile.REMSMobile.multicastreceiver.REMSMulticastReceiver;
import ch.abcm.remsmobile.REMSMobile.preferences.PrefsActivity;
import ch.bfh.abch.rems.REMSMulticastController;

public class REMSMainActivity extends Activity {

	private static final String TAG = "REMSMainActivity";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		new REMSMulticastController().startMulticastReceiver(new REMSMulticastReceiver(this));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(ch.abcm.remsmobile.REMSMobile.R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_settings:
			Intent prefIntent = new Intent(this, PrefsActivity.class);
			startActivity(prefIntent);
			break;
		}
		return true;
	}

	public void onButtonClick(View view) {
		Log.d(TAG, "Button pressed: " + view.getId());
		switch (view.getId()) {
		case R.id.bu_show_prefs:
			displaySharedPreferences();
			break;
		case R.id.bu_send_testsms:
			String testSMSMessage = getResources().getString(R.string.test_sms_message_text);
			Intent sentTestSMSIntent = ExternNotifierController.getSendTestSMSIntent(testSMSMessage);
			sendBroadcast(sentTestSMSIntent);
			
			break;
		}
	}

	private void displaySharedPreferences() {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

		String periodicNotification = prefs.getString("PeriodicNotifications", "Preiodic not found");
		String measurementDivergence = prefs.getString("MeasurementDivergence", "measurement divergence not found");
		String systemError = prefs.getString("SystemError", "systemerror not found");
		String recepient = prefs.getString("Recipient", "systemerror not found");
		String listPrefs = prefs.getString("Notifications", "List not foiund");

		StringBuilder builder = new StringBuilder();
		builder.append("periodicNotification: " + periodicNotification + "\n");
		builder.append("measurementDivergence: " + measurementDivergence + "\n");
		builder.append("systemError: " + systemError + "\n");
		builder.append("recepient: " + recepient + "\n");
		builder.append("List preference: " + listPrefs);

		Toast.makeText(this, builder.toString(), Toast.LENGTH_LONG).show();
	}
}
